﻿using System;
using System.ComponentModel;
using System.Text;
using System.Text.RegularExpressions;
using System.Windows.Forms;

namespace STT
{
    public class DialogBoxFind : Form
    {
        private IContainer components;
        private GroupBox groupBox1;
        private CheckBox checkBox_regular_expression;
        private TextBox textBox_find_what;
        private GroupBox groupBox2;
        private Button button_find_next;
        private CheckBox checkBox_match_case;
        private Button button_find;
        private ComboBox comboBox_Look_in;
        private CheckBox checkBox_match_whole_text;
        private GroupBox groupBox3;
        private Button button_close;

        public DialogBoxFind()
        {
            this.InitializeComponent();
            this.comboBox_Look_in.SelectedIndex = 0;
        }

        private DialogBoxFind.RequestFindText CreateRequest()
        {
            return new DialogBoxFind.RequestFindText()
            {
                word_regex = this.textBox_find_what.Text.Trim(),
                flag_use_regex = this.checkBox_regular_expression.Enabled & this.checkBox_regular_expression.Checked,
                flag_match_case = this.checkBox_match_case.Enabled & this.checkBox_match_case.Checked,
                flag_match_whole_text = this.checkBox_match_whole_text.Enabled & this.checkBox_match_whole_text.Checked
            };
        }

        private void DialogBoxFind_FormClosing(object sender, FormClosingEventArgs e)
        {
            this.Hide();
            e.Cancel = true;
        }

        private void checkBox_regular_expression_CheckedChanged(object sender, EventArgs e)
        {
            if (this.checkBox_regular_expression.Checked)
            {
                this.checkBox_match_case.Enabled = false;
                this.checkBox_match_whole_text.Enabled = false;
            }
            else
            {
                this.checkBox_match_case.Enabled = true;
                this.checkBox_match_whole_text.Enabled = true;
            }
        }

        private void button_find_next_Click(object sender, EventArgs e)
        {
            bool flag = false;
            DialogBoxFind.RequestFindText request = this.CreateRequest();
            if (request.flag_use_regex)
            {
                try
                {
                    Regex regex = new Regex(request.word_regex);
                }
                catch (ArgumentException ex)
                {
                    StringBuilder stringBuilder = new StringBuilder();
                    stringBuilder.AppendLine("The specified regular expression is invalid.");
                    stringBuilder.Append("Message: " + ex.Message);
                    int num = (int)MessageBox.Show(stringBuilder.ToString(), "Invalid Regex.", MessageBoxButtons.OK, MessageBoxIcon.Hand);
                    return;
                }
            }
            if (this.comboBox_Look_in.SelectedIndex == 0)
                flag = Program.Fenetre.tree_modules.FindNextString(request);
            else if (this.comboBox_Look_in.SelectedIndex == 1)
                flag = Program.Fenetre.struct_format_view.FindNextString(request);
            if (flag)
                return;
            int num1 = (int)MessageBox.Show("The specified text was not found.", "Find report.", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void button_find_Click(object sender, EventArgs e)
        {
            bool flag = false;
            if (this.comboBox_Look_in.SelectedIndex == 0)
                flag = Program.Fenetre.tree_modules.FindString(this.CreateRequest());
            else if (this.comboBox_Look_in.SelectedIndex == 1)
                flag = Program.Fenetre.struct_format_view.FindString(this.CreateRequest());
            if (flag)
                return;
            int num = (int)MessageBox.Show("The specified text was not found.", "Find report.", MessageBoxButtons.OK, MessageBoxIcon.Asterisk);
        }

        private void DialogBoxFind_Resize(object sender, EventArgs e)
        {
            this.Height = this.MinimumSize.Height;
        }

        private void button_close_Click(object sender, EventArgs e)
        {
            this.Close();
        }

        protected override void Dispose(bool disposing)
        {
            if (disposing && this.components != null)
                this.components.Dispose();
            base.Dispose(disposing);
        }

        private void InitializeComponent()
        {
            this.groupBox1 = new System.Windows.Forms.GroupBox();
            this.checkBox_regular_expression = new System.Windows.Forms.CheckBox();
            this.textBox_find_what = new System.Windows.Forms.TextBox();
            this.groupBox2 = new System.Windows.Forms.GroupBox();
            this.comboBox_Look_in = new System.Windows.Forms.ComboBox();
            this.button_find_next = new System.Windows.Forms.Button();
            this.checkBox_match_case = new System.Windows.Forms.CheckBox();
            this.button_find = new System.Windows.Forms.Button();
            this.checkBox_match_whole_text = new System.Windows.Forms.CheckBox();
            this.groupBox3 = new System.Windows.Forms.GroupBox();
            this.button_close = new System.Windows.Forms.Button();
            this.groupBox1.SuspendLayout();
            this.groupBox2.SuspendLayout();
            this.groupBox3.SuspendLayout();
            this.SuspendLayout();
            //
            // groupBox1
            //
            this.groupBox1.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox1.Controls.Add(this.checkBox_regular_expression);
            this.groupBox1.Controls.Add(this.textBox_find_what);
            this.groupBox1.Location = new System.Drawing.Point(12, 11);
            this.groupBox1.Name = "groupBox1";
            this.groupBox1.Size = new System.Drawing.Size(284, 63);
            this.groupBox1.TabIndex = 0;
            this.groupBox1.TabStop = false;
            this.groupBox1.Text = "查找:";
            //
            // checkBox_regular_expression
            //
            this.checkBox_regular_expression.AutoSize = true;
            this.checkBox_regular_expression.Location = new System.Drawing.Point(6, 42);
            this.checkBox_regular_expression.Name = "checkBox_regular_expression";
            this.checkBox_regular_expression.Size = new System.Drawing.Size(180, 16);
            this.checkBox_regular_expression.TabIndex = 1;
            this.checkBox_regular_expression.Text = "使用正则表达式（.NET语法）";
            this.checkBox_regular_expression.UseVisualStyleBackColor = true;
            this.checkBox_regular_expression.CheckedChanged += new System.EventHandler(this.checkBox_regular_expression_CheckedChanged);
            //
            // textBox_find_what
            //
            this.textBox_find_what.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox_find_what.Location = new System.Drawing.Point(6, 18);
            this.textBox_find_what.Name = "textBox_find_what";
            this.textBox_find_what.Size = new System.Drawing.Size(272, 21);
            this.textBox_find_what.TabIndex = 0;
            //
            // groupBox2
            //
            this.groupBox2.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox2.Controls.Add(this.comboBox_Look_in);
            this.groupBox2.Location = new System.Drawing.Point(12, 79);
            this.groupBox2.Name = "groupBox2";
            this.groupBox2.Size = new System.Drawing.Size(284, 48);
            this.groupBox2.TabIndex = 1;
            this.groupBox2.TabStop = false;
            this.groupBox2.Text = "结果显示在:";
            //
            // comboBox_Look_in
            //
            this.comboBox_Look_in.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
            this.comboBox_Look_in.FormattingEnabled = true;
            this.comboBox_Look_in.Items.AddRange(new object[] {
            "Tree view",
            "Format view"});
            this.comboBox_Look_in.Location = new System.Drawing.Point(6, 18);
            this.comboBox_Look_in.Name = "comboBox_Look_in";
            this.comboBox_Look_in.Size = new System.Drawing.Size(272, 20);
            this.comboBox_Look_in.TabIndex = 0;
            //
            // button_find_next
            //
            this.button_find_next.Location = new System.Drawing.Point(94, 197);
            this.button_find_next.Name = "button_find_next";
            this.button_find_next.Size = new System.Drawing.Size(76, 23);
            this.button_find_next.TabIndex = 2;
            this.button_find_next.Text = "查找下一个";
            this.button_find_next.UseVisualStyleBackColor = true;
            this.button_find_next.Click += new System.EventHandler(this.button_find_next_Click);
            //
            // checkBox_match_case
            //
            this.checkBox_match_case.AutoSize = true;
            this.checkBox_match_case.Location = new System.Drawing.Point(6, 16);
            this.checkBox_match_case.Name = "checkBox_match_case";
            this.checkBox_match_case.Size = new System.Drawing.Size(84, 16);
            this.checkBox_match_case.TabIndex = 3;
            this.checkBox_match_case.Text = "区分大小写";
            this.checkBox_match_case.UseVisualStyleBackColor = true;
            //
            // button_find
            //
            this.button_find.Location = new System.Drawing.Point(12, 197);
            this.button_find.Name = "button_find";
            this.button_find.Size = new System.Drawing.Size(76, 23);
            this.button_find.TabIndex = 2;
            this.button_find.Text = "查找";
            this.button_find.UseVisualStyleBackColor = true;
            this.button_find.Click += new System.EventHandler(this.button_find_Click);
            //
            // checkBox_match_whole_text
            //
            this.checkBox_match_whole_text.AutoSize = true;
            this.checkBox_match_whole_text.Location = new System.Drawing.Point(6, 37);
            this.checkBox_match_whole_text.Name = "checkBox_match_whole_text";
            this.checkBox_match_whole_text.Size = new System.Drawing.Size(72, 16);
            this.checkBox_match_whole_text.TabIndex = 4;
            this.checkBox_match_whole_text.Text = "全字匹配";
            this.checkBox_match_whole_text.UseVisualStyleBackColor = true;
            //
            // groupBox3
            //
            this.groupBox3.Anchor = ((System.Windows.Forms.AnchorStyles)(((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Left)
                  | System.Windows.Forms.AnchorStyles.Right)));
            this.groupBox3.Controls.Add(this.checkBox_match_whole_text);
            this.groupBox3.Controls.Add(this.checkBox_match_case);
            this.groupBox3.Location = new System.Drawing.Point(12, 133);
            this.groupBox3.Name = "groupBox3";
            this.groupBox3.Size = new System.Drawing.Size(284, 58);
            this.groupBox3.TabIndex = 5;
            this.groupBox3.TabStop = false;
            this.groupBox3.Text = "选项:";
            //
            // button_close
            //
            this.button_close.Anchor = ((System.Windows.Forms.AnchorStyles)((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Right)));
            this.button_close.Location = new System.Drawing.Point(220, 197);
            this.button_close.Name = "button_close";
            this.button_close.Size = new System.Drawing.Size(76, 23);
            this.button_close.TabIndex = 6;
            this.button_close.Text = "关闭";
            this.button_close.UseVisualStyleBackColor = true;
            this.button_close.Click += new System.EventHandler(this.button_close_Click);
            //
            // DialogBoxFind
            //
            this.AutoScaleDimensions = new System.Drawing.SizeF(6F, 12F);
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.BackColor = System.Drawing.SystemColors.Control;
            this.ClientSize = new System.Drawing.Size(308, 234);
            this.Controls.Add(this.button_close);
            this.Controls.Add(this.groupBox3);
            this.Controls.Add(this.button_find);
            this.Controls.Add(this.button_find_next);
            this.Controls.Add(this.groupBox2);
            this.Controls.Add(this.groupBox1);
            this.Cursor = System.Windows.Forms.Cursors.Default;
            this.MaximizeBox = false;
            this.MinimumSize = new System.Drawing.Size(297, 256);
            this.Name = "DialogBoxFind";
            this.Text = "查找";
            this.TopMost = true;
            this.FormClosing += new System.Windows.Forms.FormClosingEventHandler(this.DialogBoxFind_FormClosing);
            this.Resize += new System.EventHandler(this.DialogBoxFind_Resize);
            this.groupBox1.ResumeLayout(false);
            this.groupBox1.PerformLayout();
            this.groupBox2.ResumeLayout(false);
            this.groupBox3.ResumeLayout(false);
            this.groupBox3.PerformLayout();
            this.ResumeLayout(false);
        }

        public struct RequestFindText
        {
            public string word_regex;
            public bool flag_use_regex;
            public bool flag_match_case;
            public bool flag_match_whole_text;
        }
    }
}